package cn.platform.service.impl;

import cn.platform.entity.SchAnnounce;
import cn.platform.entity.UserAccount;
import cn.platform.mapper.AdminMapper;
import cn.platform.mapper.SchAnnounceMapper;


import cn.platform.mapper.TeacherMapper;
import cn.platform.request.SchAnnounceQueryDTO;
import cn.platform.response.PageResult;
import cn.platform.response.SchAnnounceVO;
import cn.platform.service.SchAnnounceService;
import cn.platform.util.identify.SessionToken;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchAnnounceServiceImpl implements SchAnnounceService {

    @Autowired
    private SchAnnounceMapper schAnnounceMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 根据条件实现分页查询
     * @param announcementQueryDTO
     * @return
     * @author oyq
     */
    @Override
    public PageResult<SchAnnounceVO> page(SchAnnounceQueryDTO announcementQueryDTO) {
        PageHelper.startPage(announcementQueryDTO.getPage(),announcementQueryDTO.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("title",announcementQueryDTO.getTitle());
        map.put("publisher",announcementQueryDTO.getPublisher());
        if(announcementQueryDTO.getStartTime()!=null){
            map.put("startTime", LocalDateTime.of(announcementQueryDTO.getStartTime(), LocalTime.MIN));
        }
        if(announcementQueryDTO.getEndTime()!=null){
            map.put("endTime",LocalDateTime.of(announcementQueryDTO.getEndTime(), LocalTime.MAX));
        }
        Page<SchAnnounceVO> page =schAnnounceMapper.pageQuery(map);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 查看公告详情
     * @param id
     * @return
     * @author oyq
     */
    @Override
    public SchAnnounceVO detail(Integer id) {
        SchAnnounce schAnnounce = schAnnounceMapper.getById(id);
        String type= schAnnounce.getPubType();
        String publisher = null;
        if(type == "管理员"){
            publisher=adminMapper.getNameById(schAnnounce.getPubId());
        }else {
            publisher=teacherMapper.getNameById(schAnnounce.getPubId());
        }
        SchAnnounceVO schAnnounceVO = new SchAnnounceVO();
        BeanUtils.copyProperties(schAnnounce,schAnnounceVO);
        schAnnounceVO.setPublisher(publisher);
        return schAnnounceVO;
    }

    /**
     * 新增公告
     * @param schAnnounce
     */
    @Override
    public void save(SchAnnounce schAnnounce) {

        UserAccount currentUser = SessionToken.getUserAccount();
        if(currentUser.getRole().equals("admin")){
            schAnnounce.setPubType("管理员");
        }else if(currentUser.getRole().equals("teacher")){
            schAnnounce.setPubType("教师");
        }
        schAnnounce.setPubId(currentUser.getId());


        schAnnounce.setIsTop(0);
        schAnnounce.setStatus("生效");
        schAnnounce.setPubTime(LocalDateTime.now());
        schAnnounce.setUpdateTime(LocalDateTime.now());
        schAnnounce.setPageViews(0);
        schAnnounceMapper.insertSchAnnounce(schAnnounce);
    }

    /**
     * 更新公告
     * @param schAnnounce
     */
    @Override
    public void update(SchAnnounce schAnnounce) {
        schAnnounce.setUpdateTime(LocalDateTime.now());
        schAnnounceMapper.updateSchAnnounce(schAnnounce);
    }

    /**
     * 删除单条公告记录
     * @param id
     */
    @Override
    public void delete(Integer id) {
        schAnnounceMapper.deleteById(id);
    }

    /**
     * 批量删除公告
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        schAnnounceMapper.deleteBatch(ids);
    }

    @Override
    public void toggleTop(SchAnnounce schAnnounce) {
        schAnnounce.setUpdateTime(LocalDateTime.now());
        schAnnounceMapper.updateSchAnnounce(schAnnounce);
    }

    @Override
    public void toggleStatus(SchAnnounce schAnnounce) {
        schAnnounce.setUpdateTime(LocalDateTime.now());
        schAnnounceMapper.updateSchAnnounce(schAnnounce);
    }
}
